from flask_sqlalchemy import SQLAlchemy
import time
from flask_mail import Mail, Message
import json
# from tencentcloud.common import credential
# from tencentcloud.common.profile.client_profile import ClientProfile
# from tencentcloud.common.profile.http_profile import HttpProfile
# from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# from tencentcloud.sms.v20210111 import sms_client, models
import random

db = SQLAlchemy()
mail = Mail()


def get_time():
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


def to_json_all(msg: list):
    data = []
    if type(msg) == list:
        for i in range(len(msg)):
            temp_dict = {}
            j = 0
            for k, v in msg[i].__dict__.items():
                if j > 0:
                    temp_dict[k] = v
                j += 1

            data.append(temp_dict)
    else:
        temp_dict = {}
        j = 0
        for k, v in msg.__dict__.items():
            if j > 0:
                temp_dict[k] = v
            j += 1
        data = temp_dict
    return data


def send_mail(subject, to, body, ctx):
    try:
        ctx.push()
        message = Message(subject, recipients=to, body=body)  # recipients=[to]
        mail.send(message)
        ctx.pop()
    except:
        send_mail(subject, to, body, ctx)


def get_verification_code():
    # list1 = []
    str_ = ""
    for i in range(4):
        yan = random.randint(0, 9)
        # list1.append(yan)
        str_ += str(yan)
    return str_
